package com.jurenzhao.partssystem.information.cars.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jurenzhao.partssystem.information.basic_series.util.PathImg;
import com.jurenzhao.partssystem.information.cars.entity.CarParts;
import com.jurenzhao.partssystem.information.cars.entity.PartsType;
import com.jurenzhao.partssystem.information.cars.entity.PartsView;
import com.jurenzhao.partssystem.information.cars.service.CarPartsService;
import com.jurenzhao.partssystem.information.cars.service.PartsTypeService;
import com.jurenzhao.partssystem.system.system_user.entity.System_Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/carparts")
public class CarPartsController {
    @Resource
    private CarPartsService carPartsService;
    @Resource
    private PartsTypeService partsTypeService;
    @RequestMapping("/delete/{id}/{name}")
    public String deleteById(@PathVariable("id") Integer id,@PathVariable("name") String name){
        System.out.println("===="+name);
        carPartsService.deleteCarParts(id);
        return "forward:/carparts/alllist/"+name+"/"+"1";
    }
    @RequestMapping("/list")
    public String CarPartsList(Model model){
        /*List<CarParts> cars = carPartsService.findAllCarParts();
        model.addAttribute("cars",cars);*/
        //查找所有的配件类型
        List<PartsView> list=new ArrayList<PartsView>(0);
        List<PartsType> types = partsTypeService.findAllPartsType();
        for (PartsType i:types) {
            PartsView partsView=new PartsView();
            partsView.setPartsType(i);
            List<CarParts> parts = carPartsService.findAllCarPartsByTypeId(i.getPartsId());
            partsView.setCarPartslist(parts);
            list.add(partsView);
        }
        model.addAttribute("list",list);
//       model.addAttribute("types",types);
//       List<CarParts> parts = carPartsService.findAllCarPartsByTypeId(1);
//        model.addAttribute("parts",parts);
        return "information/CarParts/CarParts_list";
    }
    @RequestMapping("/alllist/{name}/{page}")
    public String CarParts(HttpSession httpSession,Model model, @PathVariable("name") String name,
                           @PathVariable("page") int page){
        PageHelper.startPage(page,5);
        List<CarParts> cars = carPartsService.findAllCarPartsByName(name);
        PageInfo<CarParts> p = new PageInfo<CarParts>(cars,5);
        System_Users systemUsers = (System_Users) httpSession.getAttribute("system_users");
        String rolename = systemUsers.getUserRoleString();
        //System.out.println("--------------"+rolename);
        model.addAttribute("rolename",rolename);
        model.addAttribute("name",name);
        model.addAttribute("p",p);
        model.addAttribute("cars",cars);
        return "/information/CarParts/CartPartsAll";
    }
    @RequestMapping("/preAdd")
    public String preCarPartsAdd(Model model){
        CarParts carParts = new CarParts();
        model.addAttribute("carParts",carParts);
        //查找所有配件类型
        List<PartsType> types = partsTypeService.findAllPartsType();
        model.addAttribute("types",types);
        return "/information/CarParts/CartPartsAdd";
    }
    @RequestMapping("/add")
    public String CarPartsAdd(CarParts carParts, MultipartFile file) throws IOException {
        //图片随机名
        String imagePath = UUID.randomUUID().toString();
        File newFile = new File(PathImg.IMAGE_BASE_PATH + imagePath);
        file.transferTo(newFile);
        carParts.setPiecePic(imagePath);
        carPartsService.save(carParts);
        return "redirect:/carparts/list" ;
    }
    @RequestMapping("/preUpdate/{id}/{name}/{page}")
    public String CarPartsPreUpadate(Model model,@PathVariable("id") int id,
                                     @PathVariable("name") String name,
                                     @PathVariable("page") int page){
        CarParts carParts = new CarParts();
        System.out.println("====="+page+"==="+name);
        //获取隐藏域中的name 跟page
        model.addAttribute("page",page);
        model.addAttribute("name",name);
        model.addAttribute("carParts",carParts);
        CarParts parts =carPartsService.findAllCarPartsById(id);
        model.addAttribute("parts",parts);
        List<PartsType> types = partsTypeService.findAllPartsType();
        System.out.println(types);
        model.addAttribute("types",types);
        System.out.println("更新中！！！！！");
        return "/information/CarParts/CartPartsUpdate";
    }
    @RequestMapping("/update")
    public String CarPartsUpadate(CarParts carParts,MultipartFile file,String name,int page) throws IOException {
        String imagePath = UUID.randomUUID().toString();
        System.out.println(name+""+page);
        File newFile = new File(PathImg.IMAGE_BASE_PATH + imagePath);
        file.transferTo(newFile);
        //判读文件是否为空
        if (file.isEmpty())
        {
            carParts.setPiecePic(null);
        }
        else {
            carParts.setPiecePic(imagePath);
        }
        carPartsService.updateById(carParts);

        return "forward:/carparts/alllist/"+name+"/"+page;

        //return "redirect:/carparts/list";
    }
    @RequestMapping("/search/{page}")
    public String CarPartsSerach(Model model,@PathVariable("page") int page){
        PageHelper.startPage(page,6);
        List<CarParts> parts = carPartsService.findAll();
        PageInfo<CarParts> p = new PageInfo<CarParts>(parts,5);
        model.addAttribute("p",p);
        model.addAttribute("parts",parts);
        return "/information/CarParts/CarPartsSearch";
    }
    @RequestMapping("/searchNumGao/{page}")
    public String SortNumByGao(Model model,@PathVariable("page") int page){
        PageHelper.startPage(page,6);
        List<CarParts> parts = carPartsService.SortByNumGao();
        PageInfo<CarParts> p = new PageInfo<CarParts>(parts,5);
        model.addAttribute("p",p);
        model.addAttribute("parts",parts);
        return "/information/CarParts/CarPartsSearch";
    }
    @RequestMapping("/searchNumDi/{page}")
    public String SortNumByDi(Model model,@PathVariable("page") int page){
        PageHelper.startPage(page,6);
        List<CarParts> parts = carPartsService.SortByNumDi();
        PageInfo<CarParts> p = new PageInfo<CarParts>(parts,5);
        model.addAttribute("p",p);
        model.addAttribute("parts",parts);
        return "/information/CarParts/CarPartsSearch";
    }
    @RequestMapping("/searchPriceGao/{page}")
    public String searchPriceGao(Model model,@PathVariable("page") int page){
        PageHelper.startPage(page,6);
        List<CarParts> parts = carPartsService.SortByPriceGao();
        PageInfo<CarParts> p = new PageInfo<CarParts>(parts,5);
        model.addAttribute("p",p);
        model.addAttribute("parts",parts);
        return "/information/CarParts/CarPartsSearch";
    }
    @RequestMapping("/searchPriceDi/{page}")
    public String searchPriceDi(Model model,@PathVariable("page") int page){
        PageHelper.startPage(page,6);
        List<CarParts> parts = carPartsService.SortByPriceDi();
        PageInfo<CarParts> p = new PageInfo<CarParts>(parts,5);
        model.addAttribute("p",p);
        model.addAttribute("parts",parts);
        return "/information/CarParts/CarPartsSearch";
    }
    @RequestMapping("/keyword/{page}")
    public String searchkeyword( @PathVariable("page") int page, String ci,Model model){
        System.out.println("==="+ci);
        PageHelper.startPage(page,6);
        List<CarParts> parts = carPartsService.SearchKeyword(ci);

        PageInfo<CarParts> p = new PageInfo<CarParts>(parts,5);
        model.addAttribute("parts",parts);
        model.addAttribute("p",p);
        if(parts.isEmpty()){
            return "/information/CarParts/no";
        }else{
            return "/information/CarParts/CarPartsSearch";
        }

    }

    /*@RequestMapping("/keywordll/{page}/{ci}")
    public String keywordll(Model model,@PathVariable("page")int page,@PathVariable("ci")String ci){
        System.out.println("kk==="+ci);
        //System.out.println("kk==="+page);
        PageHelper.startPage(page,6);
        List<CarParts> parts = carPartsService.SearchKeyword(ci);
        PageInfo<CarParts> p = new PageInfo<CarParts>(parts,5);
        model.addAttribute("p",p);
        model.addAttribute("parts",parts);
        return "/information/CarParts/CarPartsSearch";
    }*/
}
